﻿using CIS.Client.Models;
using CIS.Client.Modules.Main.Views;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR.Client;
using ImTools;
using System.Windows;
using CIS.Common.Dtos;
using DryIoc.Messages;
using Newtonsoft.Json;
using System.Collections.Concurrent;
using Prism.Events;
using CIS.Client.Common.Events;
using MediatR;
using CIS.Common.Dtos.ReportDtos;
using CIS.Client.Common.Popups;

namespace CIS.Client.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        private HubConnection? hubConnection;
        private IEventAggregator _eventAggregator;
        private MessageManager _messageManager { get { return new  MessageManager(); } }
        public MainWindowViewModel(IEventAggregator eventAggregator)
        {
            _eventAggregator = eventAggregator;            
            Init();
        }

        #region 属性

        #endregion

        #region 命令

        #endregion

        #region 方法
        /// <summary>
        /// 初始化
        /// </summary>
        private async void Init()
        {
            hubConnection = new HubConnectionBuilder().WithUrl("https://localhost:5000/CISEventHub").WithAutomaticReconnect().Build();
            hubConnection.KeepAliveInterval = TimeSpan.FromSeconds(5);
            Listen();
            try
            {
                await hubConnection.StartAsync();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 监听服务端消息
        /// </summary>
        private void Listen()
        {
            hubConnection?.On<Dictionary<string, object>>("Data", ReceiveInfos);
        }

        /// <summary>
        /// 获取服务端消息处理方法
        /// </summary>
        /// <param name="data"></param>
        private void ReceiveInfos(Dictionary<string, object> data)
        {
            if (data != null)
            {
                if (Convert.ToString(data["EventName"]) == "UnitStatusChangeReport")
                {
                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        //_messageManager.ShowMessageTip("Received Unit Status Change Report",MainWindow,1);
                    });
                    var unitStatus = JsonConvert.DeserializeObject<UnitStatusChangeReportDto>(data["Status"]?.ToString());
                    _eventAggregator.GetEvent<UnitStatusChangeReportEvent>().Publish(unitStatus);
                }
                if (Convert.ToString(data["EventName"]) == "PortStatusChangeReport")
                {
                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        _messageManager.ShowMessageTip("Received Port Status Change Report");
                    });
                    var portStatus = JsonConvert.DeserializeObject<PortStatusChangeReportDto>(data["Status"]?.ToString());
                    _eventAggregator.GetEvent<PortStatusChangeReportEvent>().Publish(portStatus);
                }
            }            
        }
        #endregion




    }
}
